package com.puyuba.puyuba.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.puyuba.puyuba.R;
import com.pyb.java.basemodule.BaseActivity;
import com.puyuba.puyuba.bean.MsgDetailBean;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.network.RequestBodyUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import com.pyb.java.network.scheduler.RxSchedulers;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.commonutil.ToastHelper;


import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

public class MsgListActivity extends BaseActivity {
    
    private ImageView ivBack;
    private TextView tvTitle;
    private TextView tvTime;
    private TextView tvMsgTitle;
    private TextView tvContent;

    @Override
    public int getLayoutId() {
        return R.layout.activity_msglist;
    }

    public static void startActivity(Context context,int id) {
        Intent intent = new Intent(context, MsgListActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvMsgTitle = (TextView) findViewById(R.id.tv_msg_title);
        tvContent = (TextView) findViewById(R.id.tv_content);

        int id = getIntent().getIntExtra("id", 0);
        getmsgdetail(id, CacheUserInfo.getInstance().getId());

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    //获取消息列表
    public void getmsgdetail(int id,int uid) {
        showLoadingDialog();
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("uid", uid);
        RequestBody bodyByMap = RequestBodyUtil.getRequestBodyByMap(map);
        RetrofitManger.getInstance().createService(UserApi.class)
                .getmsgdetail(bodyByMap)
                .compose(RxSchedulers.<ResponseEntity<MsgDetailBean>>applySchedulers())
                .subscribe(new ResponseObserve<MsgDetailBean>(){

                    @Override
                    public void onSuccess(ResponseEntity<MsgDetailBean> responseEntity) {
                        MsgDetailBean data = responseEntity.getData();
                        tvTime.setText(data.getTime_str());
                        tvMsgTitle.setText(data.getTitle());
                        tvContent.setText(data.getContent());
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) { }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        closeLoadingDialog();

                    }

                });
    }

}
